//
//  GZ_Music.m
//  B段项目
//
//  Created by 高哲 on 15/11/5.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "GZ_Music.h"

static GZ_Music *music;
@implementation GZ_Music

+ (GZ_Music *)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        music = [[GZ_Music alloc] init];
        
    });
    return music;
}
-(void)playMusicWith:(NSString *)urlString
{
    if ([_cuttentURL isEqualToString:urlString]) {
        if (_flag == 0) {
            [_player pause];
            _flag = 1;
        }else
        {
            [_player play];
            [[SHD_music palyMusic]stopMusic];
            _flag = 0;
        }
    }
    else
    {
        //记录当前歌曲URl
        _cuttentURL = urlString;
        //先暂停前一首歌
        [_player pause];
        //初始化播放器
        _player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:urlString]];
        //开始播放
        [[SHD_music palyMusic]stopMusic];
        [_player play];
    }
    
    __block AVPlayer *currentPlayer = _player;
    
    __block GZ_Music *music = [GZ_Music shareInstance];
    [_player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        float currentTime = CMTimeGetSeconds(currentPlayer.currentTime);
        
        //        NSLog(@"当前已经播放: %.2f", currentTime);
        //        NSLog(@"总时间: %f", CMTimeGetSeconds(currentPlayer.currentItem.duration));
        
        //        [music.delegate passValue:currentTime / CMTimeGetSeconds(currentPlayer.currentItem.duration) passTotleTime:CMTimeGetSeconds(currentPlayer.currentItem.duration) / 60.0 passCurrentTime:currentTime];
        
       music.block(currentTime / CMTimeGetSeconds(currentPlayer.currentItem.duration), CMTimeGetSeconds(currentPlayer.currentItem.duration) / 60.0, currentTime / 60.0);
    }];
}
-(void)stopMusic
{
    [_player pause];
}
-(void)playMusic
{
    [_player play];
}



@end
